<template>
  <div>
    <el-card >
    <el-card class="audit-card" shadow="hover" :body-style="{ padding: '20px 0' }">
      <!--上方搜索框-->
      <el-form :inline="true" :model="keyword" ref="searchForm" class="demo-form-inline">
        <el-form-item label="流水号" prop="serialNumber">
          <el-input size="small" clearable v-model="keyword.serialNumber" placeholder="流水号"></el-input>
        </el-form-item>
        <el-form-item label="用户ID" prop="uid">
          <el-input size="small" clearable v-model="keyword.uid" placeholder="用户ID"></el-input>
        </el-form-item>
        <el-form-item label="用户昵称" prop="uname">
          <el-input size="small" clearable v-model="keyword.uname" placeholder="用户昵称"></el-input>
        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
          <el-input size="small" clearable v-model="keyword.phone" placeholder="手机号码"></el-input>
        </el-form-item>
        <el-form-item label="审核类型" prop="auditType">
          <el-input size="small" clearable v-model="keyword.auditType" placeholder="审核类型"></el-input>
        </el-form-item>
        <el-form-item label="提交时间" prop="submitTime">
          <el-input size="small" clearable v-model="keyword.submitTime" placeholder="提交时间"></el-input>
        </el-form-item>
        <el-form-item label="审核状态" prop="status">
          <el-input size="small" clearable v-model="keyword.status" placeholder="审核状态"></el-input>
        </el-form-item>
        <el-form-item label="审核人" prop="statusPerson">
          <el-input size="small" clearable v-model="keyword.statusPerson" placeholder="审核人"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" size="small" @click="search" icon="el-icon-search">查询</el-button>
          <el-button type="primary" plain size="small" @click="resetSearchForm('searchForm')"
                     icon="el-icon-refresh">
            重置
          </el-button>
        </el-form-item>
      </el-form>

      <!--中间表格区域-->
      <el-table v-loading="loading" :data="userManagementList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="id" align="center" prop="id" />
        <el-table-column label="流水号" align="center" prop="serialNumber" />
        <el-table-column label="用户名" align="center" prop="uname" />
        <el-table-column label="用户id" align="center" prop="uid" />
        <el-table-column label="手机号码" align="center" prop="phone" />
        <el-table-column label="审核类型" align="center" prop="auditType" />
        <!-- 新信息列 -->
        <el-table-column label="新信息" align="center">
          <template slot-scope="scope">
            <el-image
              v-if="scope.row.newInformation"
              :src="scope.row.newInformation"
              :preview-src-list="[scope.row.newInformation]"
              style="width: 80px; height: 80px;"
              alt="新信息图片"
            />
            <span v-else>-</span>
          </template>
        </el-table-column>
        <!-- 旧信息列 -->
        <el-table-column label="旧信息" align="center">
          <template slot-scope="scope">
            <el-image
              v-if="scope.row.oldInformation"
              :src="scope.row.oldInformation"
              :preview-src-list="[scope.row.oldInformation]"
              style="width: 80px; height: 80px;"
              alt="旧信息图片"
            />
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column label="提交时间" align="center" prop="submitTime" />
        <el-table-column label="状态" align="center" prop="status" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改
            </el-button>

          </template>
        </el-table-column>
      </el-table>
      <!--下方分页区域-->
      <el-pagination @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page="currentPage"
                     :page-sizes="[5, 10, 20]"
                     :page-size="100"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="total">
      </el-pagination>
    </el-card>
<!--    卡片-->
    <el-card class="audit-card" shadow="hover" :body-style="{ padding: '20px 0' }">
      <el-dialog
        title="审核表单"
        :visible.sync="isAuditDialogVisible"
        width="40%"
        @close="cancel">
        <el-card class="audit-card" shadow="hover" :body-style="{ padding: '20px 0' }">
          <template #header>
            <span class="card-title">审核表单</span>
          </template>
          <div class="audit-container">
            <el-form :model="formData" label-width="120px">
              <el-form-item label="当前审核人">
                <span>{{ currentAuditor }}</span>
              </el-form-item>
              <el-form-item label="审核结果">
                <el-radio-group v-model="formData.auditResult">
                  <el-radio :label= 1>同意</el-radio>
                  <el-radio :label= 2>拒绝</el-radio>
                </el-radio-group>
              </el-form-item>
<!--              <el-form-item label="拒绝原因" v-if="formData.auditResult === 2">-->
<!--                <el-select v-model="formData.refusalReason" placeholder="请选择原因">-->
<!--                  <el-option label="原因1" value="原因1"></el-option>-->
<!--                  <el-option label="原因2" value="原因2"></el-option>-->
<!--                </el-select>-->
<!--              </el-form-item>-->
              <el-form-item  v-show="formData.auditResult === 2" label="原因">
                <el-input type="textarea" v-model="formData.refusalReason" placeholder="请输入补充说明" :rows="5">
                </el-input>
              </el-form-item>
<!--              <el-form-item label="补充说明">-->
<!--                <el-input v-model="formData.description" placeholder="请输入补充说明"></el-input>-->
<!--              </el-form-item>-->

              <el-form-item  v-show="formData.auditResult === 2" label="详细补充说明">
                <el-input type="textarea" v-model="formData.description" placeholder="请输入补充说明" :rows="5"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="submit">确定</el-button>
                <el-button @click="cancel">取消</el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-card>
      </el-dialog>

    </el-card>

    </el-card>
  </div>
</template>

<script>
import { getList ,update } from '@/api/client/userManagement'
import format from "element-ui/src/locale/format";
export default {
  name: "Unit",
  data() {
    return {
      formData:{
          auditResult: 1,
          refusalReason:"",
          description: ""
      },
      isAuditDialogVisible: false,
      //审核
      currentAuditor: '周审核',
      auditResult: '同意',
      rejectReason: '',
      supplementaryExplanation: '',
      isExplanationVisible: false,
      //
      // 遮罩层
      loading: true,
      // 单位表格数据
      userManagementList: [],
      // 存储主单位数据
      primaryUnitList: [],
      pageNum: 1,
      pageSize: 10,
      currentPage: 1,
      total: 0,
      keyword: {
          id: null,
          serialNumber: null,
        //用户id
          uid: null,
          uname: null,
          phone: null,
          auditType: null,
          newInformation: null,
          oldInformation: null,
          submitTime: null,
          status: null,
          statusPerson: null,
           auditResult: null,
          refusalReason: null,
          description: null,
      },
      ids: [],
      title: '',
      dialogFormVisible: false,
      dialogExcelVisible: false,
      unitMeasure: {
        measureCode: '',
        measureName: '',
        primaryFlag: 'Y',
        enableFlag: 'Y',
        remark: '',
        primaryId: null,
        measureId: null,
        changeRate: 1
      },
      unitMeasureRules: {
        measureCode: [
          { required: true, message: '单位编码不能为空', trigger: 'blur' },
        ],
        measureName: [
          { required: true, message: '单位名称不能为空', trigger: 'blur' },
        ],
        primaryFlag: [
          { required: true, message: '是否是主单位不能为空', trigger: 'blur' },
        ],
        enableFlag: [
          { required: true, message: '是否默认启用不能为空', trigger: 'blur' },
        ],
      },
      isShowPrimary: false
    }
  },
  props: {
    // 定义接收后端数据的prop，假设数据是一个对象数组
    receivedData: {
      type: Array,
      default: () => []
    }
  },
  methods: {
    toggleExplanationVisibility() {
      this.isExplanationVisible = !this.isExplanationVisible;
    },
    submit() {
      // 这里可以编写提交数据到后端的逻辑，例如使用 axios 发送请求
      console.log('提交审核：', {
        id:this.keyword.id,
        auditResutl: this.formData.auditResutl,
        refusalReason: this.formData.refusalReason,
        description: this.formData.description,

      });
      console.log('提交审核前 keyword:', this.keyword);
      console.log('提交审核前 formData:', this.formData);
      let params = {
        id:this.keyword.id,

        serialNumber:  this.keyword.serialNumber,
        //用户id
        uid: this.keyword.uid,
        uname: this.keyword.uid,
        phone: this.keyword.phone,
        auditType: this.keyword.auditType,
        newInformation: this.keyword.newInformation,
        oldInformation: this.keyword.oldInformation,
        submitTime: this.keyword.submitTime,
        status: this.keyword.status,
        statusPerson: this.keyword.statusPerson,
        auditResult: this.formData.auditResult ,
        refusalReason: this.formData.refusalReason,
        description: this.formData.description,
      }
      update(params).then(res => {
        if(res.code === 200){

          this.$message({
            type: 'success',
            message: '成功!'
          });
          this.isAuditDialogVisible = false;

          this.keyword = {
            id: null,
            serialNumber: null,
            uid: null,
            uname: null,
            phone: null,
            auditType: null,
            newInformation: null,
            oldInformation: null,
            submitTime: null,
            status: null,
            statusPerson: null,
            auditResult: null,
            refusalReason: null,
            description: null
          };
          this.fetchData();
        }
      }).catch(res =>{
        this.$message({
          type: 'error',
          message: '失败!'
        });
        this.isAuditDialogVisible = false;
        this.fetchData();
        for (let key in this.keyword) {
          if (this.keyword.hasOwnProperty(key)) {
            this.keyword[key] = null;
          }
        }
      })
    },
    cancel() {
      // 这里可以编写取消操作的逻辑，例如清空输入等
      this.auditResult = '同意';
      this.rejectReason = '';
      this.supplementaryExplanation = '';
      this.isExplanationVisible = false;
    },
    fetchData(){
      //分页 + 模糊查询 带到后端的参数
      let params = {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        serialNumber: this.keyword.serialNumber,
        uid: this.keyword.uid,
        uname: this.keyword.uname,
        phone: this.keyword.phone,
        auditType: this.keyword.auditType,
        newInformation: this.keyword.newInformation,
        oldInformation: this.keyword.oldInformation,
        submitTime: this.keyword.submitTime,
        status: this.keyword.status,
        statusPerson: this.keyword.statusPerson,
      }
      //axios.get(url, params).then(res => {}
      getList(params).then(res => {
        console.log("res", res)
        let pageInfo = res.data;//{total:100,dataList:[]}
        console.log("pageInfo", pageInfo)
        this.loading = false;//关闭遮罩层
        this.userManagementList = pageInfo.records;
        console.log("this.measureList", this.userManagementList)
        this.total = pageInfo.total;
      })
    },


    // 获取单位列表
    // 多选框选中数据
    handleSelectionChange(selectionRowArr) {
      this.ids = [];
      // console.log(selectionRowArr)
      for (let i = 0; i < selectionRowArr.length; i++) {
        this.ids.push(selectionRowArr[i].refundId)
      }
      // console.log("ids",this.ids)
    },
    // 修改计量单位
    handleUpdate(row) {
      // 将当前行数据赋值给keyword
      this.keyword.id = row.id;
      this.keyword.serialNumber = row.serialNumber;
      this.keyword.uid = row.uid;
      this.keyword.uname = row.uname;
      this.keyword.phone = row.phone;
      this.keyword.auditType = row.auditType;
      this.keyword.newInformation = row.newInformation;
      this.keyword.oldInformation = row.oldInformation;
      this.keyword.submitTime = row.submitTime;
      this.keyword.status = row.status;
      this.keyword.statusPerson = row.statusPerson;
      this.isAuditDialogVisible = true;
    },
    // 批量删除
    deleteBatch() {
      this.$confirm('是否确认删除单位编号为[' + this.ids + ']的数据项？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // let idStr = this.ids.join(",")
        // //console.log("idStr",idStr)
        // let params = {
        //     ids: this.ids
        // }
        // console.log("lin:660:params",params)
        delBatch(this.ids).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });

          this.fetchData();
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    // 页面大小改变时触发
    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchData()
    },
    // 当前页码改变时触发
    handleCurrentChange(val) {
      this.pageNum = val;
      this.fetchData()
    },
    // 模糊搜索
    search() {
      this.fetchData()
    },
    // 重置表单
    resetSearchForm(formName) {
      this.$refs[formName].resetFields();
      this.fetchData()
    },
    //导出
    downloadExcel() {},
  },
  created() {
    this.fetchData()
  }

}
</script>

<style >
.audit-container {
  padding: 20px;
  max-width: 600px;
  margin: 20px auto;
}
</style>
